package net.easyjava.tools.security.md5;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Md5工具，用于计算输入的md5
 * @author mahuimin
 * @version v1.0
 * 2013-05-29
 */
public class Md5Tools {
	private static Logger logger = LoggerFactory.getLogger(Md5Tools.class);
	public static String md5(String plainText) {
		String str=null;
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			str = buf.toString();
		} catch (NoSuchAlgorithmException e) {
			logger.error(e.getMessage(),e.fillInStackTrace());
		}
		return str;
	}
	
	public static void main(String[] args){
		System.out.println(md5("awdxs"));
	}
}
